<?php

namespace app\admin\controller;

class Advertisement extends Common {
        /*
         * 分类管理
         */

        public function ad_type() {
                $map = $this->_search("AdType");
                $this->_list(D("AdType"), $map);
                return $this->fetch();
        }

        /*
         * 分类添加
         */

        public function ad_type_add($typeid = '') {
                if (IS_POST) {
                        if ($typeid) {
                                $this->_update("AdType", false, false);
                        } else {
                                $this->_insert("AdType", false, false);
                        }
                } else {
                        if ($typeid) {
                                $nc = M("AdType")->field(true)->find($typeid);
                                $this->assign('nc', $nc);
                        } else {
                                $nc['status'] = 1;
                                $this->assign('nc', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 分类删除
         */

        public function ad_type_del($typeid = '') {
                $this->_delete("AdType", $typeid);
        }

        /*
         * 广告管理
         */

        public function ad_index() {
                $map = $this->_search("AdAd");
                if (I('post.adurl')) {
                        $map['adurl'] = array('like', "%" . I('post.adurl') . "%");
                }
                $this->_list(D("AdAd"), $map, 'id', false, true);
                $news_cat = M('AdType')->field('typeid,typename')->select();
                $this->assign('news_cat', $news_cat);
                $member = M('Member')->field('id,realname')->select();
                $this->assign('member', $member);
                return $this->fetch();
        }

        /*
         * 置顶/不置顶
         */

        public function ad_ischecked() {
                $id = I('get.id');
                $act = I('get.act');
                $data = array();
                if ($act == 'do') {
                        $data['state'] = '1';
                        $this->sysAdd("执行了【审核广告】操作，id=" . $id);
                } elseif ($act == 'un') {
                        $data['state'] = '0';
                        $this->sysAdd("执行了【取消审核广告】操作，id=" . $id);
                } else {
                        $this->iError("非法操作！");
                }
                $ret = M("AdAd")->where(['id' => ['in', explode(',', $id)]])->data($data)->save();
                if ($ret !== false) {
                        $this->delSuccess('操作成功');
                } else {
                        $this->iError("数据库异常，请联系管理员解决。");
                }
        }

        /*
         * 广告添加
         */

        public function advertisement_add($id='') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("AdAd", false, true);
                        } else {
                                $this->_insert("AdAd", false, true);
                        }
                } else {
                        if ($id) {
                                $nc = M("AdAd")->field(true)->find($id);
                                $this->assign('ad', $nc);
                        } else {
                                $nc=array();
                                $nc['typeid']='';
                                $nc['sort']='';
                                $nc['state']=0;
                                $nc['filename']='';
                                $this->assign('ad', $nc);
                        }
                        $news_cat = M('AdType')->field('typeid,typename')->select();
                        $this->assign('ad_cat', $news_cat);
                        return $this->fetch();
                }
        }

        /*
         * 广告删除
         */

        public function advertisement_del($id) {
                $this->_delete("AdAd", $id);
        }

        /**
         * 广告代码显示
         */
        public function codeshow() {
                $ad_id = $_REQUEST['id'];
                //type=1 为html方式调用广告  type=2 为js方式广告 
                $type = $_REQUEST['type'];
                if ($type == 1) {
                        if ($ad_id) {
                                $adModel = M("ad_ad");
                                $ret = $adModel->where("id = '" . $ad_id . "'")->find();
                                $content = "<php>echo htmlCodeShow(" . $ad_id . ");</php>";
                                $html = '<textarea name="code_sl" id="code_sl" style="width:98%;height:140px;">' . $content . '</textarea>';
                                $this->assign('html', $html);
                                return $this->fetch();
                        }
                }
                //js方式调用代码
                if ($type == 2) {
                        if ($ad_id) {
                                $adModel = M("ad_ad");
                                $ret = $adModel->where("id = '" . $_REQUEST['id'] . "'")->find();
                                $html = '<textarea name="code_sl" id="code_sl" style="width:98%;height:140px;"><script src="' . '/Tours/Avertise/index/id/' . $ret['id'] . '" charset="utf-8" type="text/javascript"></script></textarea>';
                                $this->assign('html', $html);
                                return $this->fetch();
                        }
                }
        }

}
